﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!IsPostBack)
        {
            if (Request.Form["lb"] != null)
            {
                Response.Clear();
                //登录
                if (Request.Form["lb"].ToString() == "login")
                {
                    string sUserName = Request.Form["username"].ToString();
                    string sPassword = Request.Form["password"].ToString();
                    string sConfirm = Request.Form["confirm"].ToString();
                    if (Session["CheckCode"].ToString() != sConfirm)
                    {
                        Response.Write("验证码错误");
                    }
                     //验证码正确
                    else
                    {
                        //验证账号是否存在
                        MembershipUser mu = Membership.GetUser(sUserName);
                        if (mu == null)
                        {
                            Response.Write("用户名不存在");
                        }
                        else
                        {//存在就开锁，防止有些用户多次密码尝试后被锁定。
                            mu.UnlockUser();
                            //核对密码。
                            if (Membership.ValidateUser(sUserName, sPassword))
                            {
                                FormsAuthentication.SetAuthCookie(sUserName, true);
                                WebMake.updateLogin(sUserName);
                                Response.Write("登录成功");
                            }
                            else
                            {
                                Response.Write("密码错误");
                            }
                        }
                    }
                }//end of login if
                Response.End();
            }//end of check
            //没有参数的情况检查是否登录。如果已经登录，就跑到默认页面去。
            else if (Request.QueryString["lb"] != null)
            {
                if (Request.QueryString["lb"].ToString() == "logout")
                {
                    FormsAuthentication.SignOut();
                    Response.Redirect(FormsAuthentication.LoginUrl);
                }
            }
            else
            {
                if (Page.User.Identity.Name != "")
                    Response.Redirect(FormsAuthentication.DefaultUrl);
            }
        }
    }
}
